<template>
  <div class="menu-container">
    <loopMenu
      v-if="menuData.length"
      :menuData="menuData"
      :currentMenu="currentMenu"
      @changeMenu="onClickMenu"
    ></loopMenu>
    <div
      class="sidebar"
      :style="store.showUi ? 'transform: translate(-50%, 0%)' : 'transform: translate(-50%, 100%);'"
    >
      <div
        :class="['sidebar-item', {'sidebar-action': item.name === route.meta.title}]"
        v-for="item in subMenuData"
        :key="item.id"
        @click="onClickSubMenu(item)"
      >
        <SvgIcon :icon="item.icon" class="sidebar-icon"></SvgIcon>
        <span class="menu-label">{{item.name}}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import loopMenu from "./loopMenu.vue";
import SvgIcon from "../../../components/SvgIcon.vue";
import { ref, watch, defineProps } from "vue";
import { useRouter, useRoute } from "vue-router";
import { useStore } from "../../../store/store"

const props = defineProps({
  menuData: {
    type: Array,
    default: []
  },
});

const store = useStore();
const currentMenu = ref("");
const currentSubMenu = ref("");
const router = useRouter();
const route = useRoute();
const subMenuData = ref([]);
let defaultMenu = null
setSubMenuData();
watch(route, route => {
  setSubMenuData();
});
function setSubMenuData() {
  if (route.meta && route.meta.title) {
    if (route.meta.parentTitle) {
      currentMenu.value = route.meta.parentTitle;
      currentSubMenu.value = route.meta.title;
    } else {
      currentMenu.value = route.meta.title;
      currentSubMenu.value = "";
    }
    subMenuData.value = props.menuData.find(
      item => item.name === currentMenu.value
    )?.children || [];
  } else {
    currentMenu.value = "";
    currentSubMenu.value = "";
    subMenuData.value = [];
    return;
  }
}
const onClickMenu = menu => {
  if (currentMenu.value === menu.name) return;
  if (menu.routerName) {
    router.push({ name: menu.routerName });
  } else {
    router.push("/");
  }
};
const onClickSubMenu = menu => {
  store.showWindow = true
  if (currentSubMenu.value === menu.name) return;
  if (menu.routerName) {
    router.push({ name: menu.routerName });
  } else {
    router.push("/");
  }
};
</script>

<style lang="scss" scoped>
.menu-container {
  .sidebar {
    position: absolute;
    display: flex;
    bottom: 0px;
    left: 50%;
    z-index: 999;
    background: linear-gradient(
      90deg,
      rgba(0, 0, 0, 0) 0%,
      rgba(0, 0, 0, 0.8) 15%,
      rgba(0, 0, 0, 1) 50%,
      rgba(0, 0, 0, 0.8) 85%,
      rgba(0, 0, 0, 0) 100%
    );
    padding: 0 100px;
    border-radius: 10px;
    transition: all 0.5s;
    .sidebar-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      color: #fff;
      cursor: pointer;
      font-size: 12px;
      margin: 15px 0;
      padding: 0 15px;
      border-right: 1px solid #fff;
    }
    .sidebar-item:last-child {
      border-right: none;
    }
    .sidebar-action {
      color: #ffff00;
    }
    .sidebar-icon {
      font-size: 18px;
    }
  }
  .flex-center {
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .menu-label{
    white-space: nowrap;
  }
}
</style>